package edu.zli.register;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc
 * @Author Zerlinda
 * @Date 2022/5/7 15:01
 * @Version 1.0
 *
 * 容器式单例线程安全问题
 */
public class ContainerSingleton {

    private ContainerSingleton() {}

    private static Map<String, Object> ioc = new HashMap<>();

    public static Object getInstance(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if(!ioc.containsKey(className)){
            Object instance = Class.forName(className).newInstance();
            ioc.put(className, instance);
        }

        return ioc.get(className);
    }
}
